package com.jingdianjichi.circle.server.controller;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.entity.Result;
import com.jingdianjichi.circle.api.common.PageResult;
import com.jingdianjichi.circle.api.req.GetShareMomentReq;
import com.jingdianjichi.circle.api.req.RemoveShareMomentReq;
import com.jingdianjichi.circle.api.req.SaveMomentCircleReq;
import com.jingdianjichi.circle.api.req.SaveShareCommentReplyReq;
import com.jingdianjichi.circle.api.vo.ShareMomentVO;
import com.jingdianjichi.circle.server.entity.po.ShareCircle;
import com.jingdianjichi.circle.server.entity.po.ShareMoment;
import com.jingdianjichi.circle.server.service.ShareCircleService;
import com.jingdianjichi.circle.server.service.ShareMomentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * 动态信息(ShareMoment)表控制层
 *
 * @author makejava
 * @since 2024-06-07 15:51:55
 */
@RestController
@RequestMapping("/share/moment")
@Slf4j
public class ShareMomentController {
    @Resource
    private ShareMomentService shareMomentService;

    @Resource
    private ShareCircleService shareCircleService;
    /**
     * 发布内容
     */
    @PostMapping(value = "/save")
    public Result<Boolean> save(@RequestBody SaveMomentCircleReq req) {
        try {
            if (log.isInfoEnabled()){
                log.info("发布内容入参:", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(Objects.nonNull(req), "入参不能为空");
            Preconditions.checkArgument(Objects.nonNull(req.getCircleId()), "圈子id不能为空");
            ShareCircle data = shareCircleService.getById(req.getCircleId());
            Preconditions.checkArgument(Objects.nonNull(data) && data.getParentId() != -1, "圈子不存在");
            Preconditions.checkArgument((Objects.nonNull(req.getContent()) || Objects.nonNull(req.getPicUrlList())), "内容不能为空");
            Boolean result = shareMomentService.saveMoment(req);
            if (log.isInfoEnabled()){
                log.info("发布内容结果:", JSON.toJSONString(result));
            }
            return Result.ok(result);
        } catch (IllegalArgumentException e){
            log.error("参数异常:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
        catch (Exception e) {
            log.error("发布内容异常:{}",e.getMessage(),e);
            return Result.fail("发布内容异常!");
        }
    }
    /**
     * 分页查询列表
     */
    @GetMapping(value = "/getMoments")
    public Result<PageResult<ShareMomentVO>> getMoments(@RequestBody GetShareMomentReq req){
        try {
            if (log.isInfoEnabled()){
                log.info("分页查询列表入参:", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(Objects.nonNull(req.getCircleId()), "圈子id不能为空");
            Preconditions.checkArgument(Objects.nonNull(req.getPageInfo()), "分页信息不能为空");
            PageResult<ShareMomentVO> result = shareMomentService.getMoments(req);
            if (log.isInfoEnabled()){
                log.info("分页查询列表结果:", JSON.toJSONString(result));
            }
            return Result.ok(result);
        }
        catch (IllegalArgumentException e){
            log.error("参数异常:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
        catch (Exception e){
            log.error("分页查询列表异常:{}",e.getMessage(),e);
            return Result.fail("分页查询列表异常!");
        }
    }
    /**
     * 删除内容
     */
    @PostMapping(value = "/remove")
    public Result<Boolean> remove(@RequestBody RemoveShareMomentReq req) {
        try {
            if (log.isInfoEnabled()){
                log.info("删除内容入参:", JSON.toJSONString(req));
            }
            Preconditions.checkArgument(Objects.nonNull(req), "入参不能为空");
            Preconditions.checkArgument(Objects.nonNull(req.getId()), "动态id不能为空");
            Boolean result = shareMomentService.removeMoment(req);
           if (log.isInfoEnabled()){
               log.info("删除内容结果:", JSON.toJSONString(result));
           }
           return Result.ok(result);
       }
        catch (IllegalArgumentException e){
            log.error("参数异常:{}",e.getMessage(),e);
            return Result.fail(e.getMessage());
        }
        catch (Exception e){
            log.error("删除内容异常:{}",e.getMessage(),e);
            return Result.fail("删除内容异常!");
        }
   }

}

